<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="选择配置" width="1100px" showFooter destroy-on-close @ok="handleSubmit">
    <BasicTable @register="registerTable">
      
    </BasicTable>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from "vue"
  import { BasicModal, useModalInner } from "/@/components/Modal"
  import * as tmplApi from "/@/api/infra/configTmpl"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { BasicTable, useTable, BasicTableProps } from '/@/components/Table';
  import { columns, searchFormSchema } from "./data"
  import  { ConfigStatus } from '/@/views/sys/configTmpl/data'

  type Key = string | number;

  const { createMessage } = useMessage()
  const emit = defineEmits(["success", "register"])

  const selectedRowKeys = ref<any[]>([])

  const tableProps: BasicTableProps = {
    bordered: false,
    rowSelection: {
      type: 'radio',
      columnWidth: 45,
      selectedRowKeys: selectedRowKeys as unknown as Key[],
      onChange: (_selectedRowKeys: Key[], selectedRows: Recordable[]) => {
        selectedRowKeys.value = _selectedRowKeys;
      }
    },
    alwaysShowTitle: false,
    rowKey: 'configKey',
    api: tmplApi.listTmpl,
    beforeFetch: (params) => {
      params.status = ConfigStatus.NORMAL
      return params
    },
    columns: columns,
    showIndexColumn: false,
    pagination: false,
    useSearchForm: true,
    formConfig: {
      baseColProps: { lg: 6, md: 12 },
      labelWidth: 60,
      schemas: searchFormSchema,
      showResetButton: false,
    },
    canResize: false
  };

  const [registerTable, { reload, clearSelectedRowKeys, getSelectRows }] = useTable(tableProps)
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    clearSelectedRowKeys()
    setModalProps({ 
      canFullscreen: true,
      defaultFullscreen: false,
    })
    
  })

  async function handleSubmit() {
    closeModal()
    if (selectedRowKeys.value.length == 0) {
      return
    }
    const data = getSelectRows()[0]
    const submitData = {
      type: data.type,
      configKey: data.configKey,
      diyFormId: data.diyFormId,
      name: data.name,
      remark: data.remark,
    }
    emit("success", submitData)
  }
</script>
